//Iditarod Trail
//Amelia Bauer, Elise Eiden, Christine Gerardi, Taryn Green
//CSE 20212 2012
/*Map Window that displays the progress and current position*/

#include "Map.h"
#include <iostream>
#include <stdio.h>
#include <QtGui>
#include <QLineEdit> 
#include <QPushButton>
#include <QApplication>
#include <QGridLayout>
#include <QString>
#include <QLabel>
#include <QFrame>
#include <QImage>
#include <QPixmap>
#include <QMainWindow>
#include <QPalette>
#include <QHBoxLayout>
#include <QFont>
#include <QTimer>
#include <string>
#include <cstring>
#include <QPushButton>
#include <QMouseEvent>
#include <vector>

using namespace std;


Map::Map(int loci, QTimer * timer, QWidget *parent) {
	
	clock = timer; 

	/* establish X and Y dimensions */
	int xSize = 1200;
	int ySize = 866;
	
	
	/*initialize window*/
	QString mainTitle("MAP");
	setWindowTitle(mainTitle);
	resize(xSize,ySize);
	
	
	QWidget *widget = new QWidget();
	big_layout = new QGridLayout(widget);
	
	QPalette p = palette();
	QPixmap pixmap1("Map1.jpg" );
	QSize size(xSize,ySize);
	QPixmap pixmap(pixmap1.scaled(size));
	p.setBrush(QPalette::Background,  pixmap);
	setPalette(p);
	
	/*initialize location vectors*/
	initialize();
	int location = calcLocation(loci);
	
			int x = 0;
			int y = 0;
		
		switch (location) {
			case 0: x = 964;
				y = 618;
				break;
			case 7: x = 1005;
				y = 586;
				break; 
			case 15: x = 961;
				 y = 578;
				 break;
			case 20: x = 929;
				 y = 601;
				 break;
			case 30: x = 880;
				 y = 564;
				 break;
			case 35: x = 842;
				 y = 569;
				 break;
			case 40: x = 805;
				 y = 555;
				 break;
			case 55: x = 746;
				 y = 476;
				 break;
			case 70: x = 669;
				 y = 471;
				 break;
			case 85: x = 615;
				 y = 438;
				 break;
			case 100: x = 672;
				  y = 295;
				  break;
			case 110: x = 567;
				  y = 276;
				  break;
			case 125: x = 519;
				  y = 337;
				  break;
			case 140: x = 423;
				  y = 381;
				  break;
			case 155: x = 408;
				  y = 274;
				  break;
			case 165: x = 323;
				  y = 311;
				  break;
			case 170: x = 285;
				  y = 282;
				  break;
			case 180: x = 187;
				  y = 314;
				  break;
				 }
	
	/*location dot*/			 
	QLabel *dot = new QLabel(this);
	QPixmap dotpixmap("redDot.png");
	dot->setPixmap(dotpixmap);
	dot->setFixedSize(x+40,y+40);
	dot->setAlignment(Qt::AlignBottom | Qt::AlignRight);
	//dot->setFrameStyle(QFrame::Box | QFrame::Raised);
	
	QLabel *empty = new QLabel(QString("                    "));

	/*exit button*/
	QPushButton *button = new QPushButton(QString("EXIT"),this);
	button->setFixedSize(150,30);
	
	//big_layout->addWidget(dot,0,0);
	big_layout->addWidget(empty,0,1);
	big_layout->addWidget(empty,1,0);
	big_layout->addWidget(empty,1,1);
	widget->setLayout(big_layout);
	big_layout->addWidget(button,0,1);
	setCentralWidget(widget);		

	connect(button, SIGNAL(clicked()), this, SLOT(close()));

}

void Map::close()
{
	clock->start(3000);
	this->hide();
}


void Map::mousePressEvent(QMouseEvent *e) {
	/*push points into points vector*/
	 points.push_back(e->x());
	 points.push_back(e->y()); 

	points.clear();
	update();
}

int Map::calcLocation(int current){
	for ( int i = 1; i < 18; i++) {
		if (current <= all_locations[i]) {
			int upper = all_locations[i]-current;
			int lower = current - all_locations[i-1];
			if (upper > lower) return all_locations[i-1];
			else return all_locations[i];
			}
			else continue;
		}
		return -1;
}

void Map::initialize(){
	int all[18] = {0,7,15,20,30,35,40,55,70,85,100,110,125,140,155,165,170,180};
	int store[6] = {0,15,40,125,140,180};
	
	for ( int i = 0; i < 18; i++ ){
		all_locations.push_back(all[i]);
		}
		
	for (int j = 0; j < 6; j++ ) {
		store_locations.push_back(store[j]);
		}
}
